<?php

namespace App\Http\Controllers\Admin;

use App\Http\Services\QcloudCosService;
use App\Http\Services\UploadService;
use EasyWeChat\Factory;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class UploadController extends Controller
{
    /**
     *文件上传
     * @author: lican<364548233@qq.com>
     */
    public function upload(Request $request){
        $this->validate($request, [
            'admin' => 'required',
            'file' => 'required',
        ]);

        $manager = $request->input('admin');
        $manager['school'] = ['id' => 1, 'code' => '1013957946'];
        $fileData = $request->file('file');
        $type = (int)$request->input('type') ?? UploadService::TYPE_FILE;
        $result = UploadService::uploadFile($manager, $fileData, $type);

        return $result;
    }

    /**
     *测试上传
     * @author: lican<364548233@qq.com>
     */
    public function index(Request $request){
        $this->validate($request, [
            'file' => 'required'
        ]);
        $file = $request->file('file');

        $fileName = $file->getClientOriginalName();
        $realName = $file->getRealPath();
        $extension = $file->getClientOriginalExtension();

        $name = './uploads/images/' . date('YmdHis'). str_random(). ".{$extension}";
        $path = dirname($name);
        if(!is_dir($path)){
            mkdir($path, 0777, true);
        }
        $res = $file->move($path, $name);
        print_r($res);die;
        echo sprintf("fileName: %s , realNmae: %s , extension: %s", $fileName, $realName, $extension);
    }

}
